//
//  GesDatabase.m
//  HuaMuJinRong
//
//  Created by Cindy on 15-12-29.
//  Copyright (c) 2015年 EIMS. All rights reserved.
//

#import "GesDatabase.h"
#import "FMDatabase.h"
@implementation GesDatabase
{
    FMDatabase *dataBase;
}

static GesDatabase *instance = nil;

+(GesDatabase *)getInstance
{
    @synchronized(self)
    {
        if (instance==nil)
        {
            instance = [[GesDatabase alloc]init];
        }
    }
    return instance;
}


- (id)init
{
    self = [super init];
    if (self) {
        NSArray * arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * path = [arr objectAtIndex:0];
        // xxx.db 数据库的名字
        path = [path stringByAppendingPathComponent:@"Test.db"];
        DLOG(@"path is %@...",path);
        // 根据绝对路径创建一个数据库
        dataBase = [FMDatabase databaseWithPath:path];
        // 打开数据库
        if (![dataBase open]) {
            DLOG(@"can not open dataBase!");
        }
        [dataBase executeUpdate:@"CREATE TABLE if not exists user_info (user_name text PRIMARY KEY,login_pwd text,gesture_pwd text,gesture_state text)"];
        
    }
    return self;
}



- (void)insertOrUpdateUserInfoWithUserName:(NSString *)userName loginPwd:(NSString *)loginpwd gesturePwd:(NSString *)gesturePwd gestureState:(NSString *)gestureState
{
    NSString *sql=[NSString stringWithFormat:@"select * from user_info Where user_name='%@'",userName];
    FMResultSet *rs1=[dataBase executeQuery:sql];
    
    if([rs1 next])
    {
        [dataBase executeUpdate:@"UPDATE user_info SET login_pwd=?, gesture_pwd=?, gesture_state=? WHERE user_name=?",loginpwd,gesturePwd,gestureState,userName];
    }
    
    else
    {
        [dataBase executeUpdate:@"INSERT INTO user_info (user_name,login_pwd,gesture_pwd,gesture_state) VALUES (?,?,?,?)",userName,loginpwd,gesturePwd,gestureState];
    }
    
    [rs1 close];
}

- (void)updateGestureState:(NSString *)userName gestureState:(NSString *)gestureState
{
    [dataBase executeUpdate:@"UPDATE user_info SET gesture_state=? WHERE user_name=?",gestureState,userName];
}

- (void)updateGesturePwdWithUser:(NSString *)userName gesture:(NSString *)gesturePwd
{
     [dataBase executeUpdate:@"UPDATE user_info SET gesture_pwd=? WHERE user_name=?",gesturePwd,userName];
}
- (NSString *)getLoginPwd:(NSString *)userName
{
    NSString *sql=[NSString stringWithFormat:@"select login_pwd from user_info Where user_name='%@'",userName];
    FMResultSet *rs1=[dataBase executeQuery:sql];
    
    NSString *pwd=nil;
    
    if([rs1 next])
    {
        pwd=[rs1 stringForColumnIndex:0];
    }
    
    
    
    [rs1 close];
    
    return pwd;
}


- (NSMutableArray *)getAllUserNames
{
    NSMutableArray *userNames=[NSMutableArray array];
    
    NSString *sql=@"select user_name from user_info";
    
    FMResultSet *rs1=[dataBase executeQuery:sql];

    
    while([rs1 next])
    {
        NSString *userName=[rs1 stringForColumnIndex:0];
        
        [userNames addObject:userName];
    }
    
    [rs1 close];
    
    return userNames;

}
- (NSString *)getGesturePwd:(NSString *)userName
{
    NSString *sql=[NSString stringWithFormat:@"select gesture_pwd from user_info Where user_name='%@'",userName];
    FMResultSet *rs1=[dataBase executeQuery:sql];
    
    NSString *pwd=nil;
    
    if([rs1 next])
    {
        pwd=[rs1 stringForColumnIndex:0];
    }
    
    
    
    [rs1 close];
    
    return pwd;
}


- (NSString *)getGestureState:(NSString *)userName
{
    NSString *sql=[NSString stringWithFormat:@"select gesture_state from user_info Where user_name='%@'",userName];
    FMResultSet *rs1=[dataBase executeQuery:sql];
    
    NSString *gestureState=@"-1";
    
    if([rs1 next])
    {
        gestureState=[rs1 stringForColumnIndex:0];
    }
    
    
    
    [rs1 close];
    
    return gestureState;
}

- (void)deleteUserInfoWithUser:(NSString *)account
{
    NSString *mysql=[NSString stringWithFormat:@"delete from user_info Where user_name='%@'",account];

    [dataBase executeUpdate:mysql];
}


- (void)deleteAllUserInfo
{
    NSString *mysql=@"delete from user_info";
    
    [dataBase executeUpdate:mysql];
}
@end
